#include <iostream>

using namespace std;
int n, m;

const int N = 10100;
int arr[N];
int mars[N];
bool st[N];
int res;
bool flag = false;
void dfs(int x)
{
    //剪枝
    if (flag)
    {
        return;
    }
    if (x > n)
    {
        res++;
        if (m + 1 == res)
        {
            flag = true;
            for (int i = 1; i <= n; i++)
            {
                cout << arr[i] << " ";
            }
        }
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        if (!res)
        {
            i = mars[x];
        }
        if (!st[i])
        {
            st[i] = true;
            arr[x] = i;
            dfs(x + 1);
            st[i] = false; // 恢复
            arr[x] = 0;
        }
    }
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;


    for (int i = 1; i <= n; i++)
    {
        cin >> mars[i];
    }
    dfs(1);
    return 0;
}